<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_Zend
 * @subpackage Controller
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * @see Zend_Controller_Action_Helper_Redirector
 */
require_once 'Zend/Controller/Action/Helper/Redirector.php';
/**
 * @see SmartL_Application
 */
require_once 'SmartL/Application.php';

/**
 * @package SmartL_Zend
 * @subpackage Controller
 */
class SmartL_Zend_Controller_Action_Helper_Redirector extends Zend_Controller_Action_Helper_Redirector {
	/**
	 * Perform a redirect to an action/controller/module with params.
	 * In SF it is rewriten to use setGoto.
	 *
	 * @param  string $action
	 * @param  string $controller
	 * @param  string $module
	 * @param  array  $params
	 * @return void
	 */
	public function gotoSimple($action, $controller = null, $module = null, array $params = array()) {
		$this->setGoto ( $action, $controller, $module, $params );
	}
	
	/**
	 * Set a redirect URL of the form /module/controller/action/params
	 *
	 * @param  string $action
	 * @param  string $controller
	 * @param  string $module
	 * @param  array  $params
	 * @return void
	 */
	public function setGoto($action, $controller = null, $module = null, array $params = array()) {
		$dispatcher = Zend_Controller_Front::getInstance ()->getDispatcher ();
		$request = $this->getRequest ();
		
		if (null === $controller) {
			$controller = $request->getControllerName ();
			if (empty ( $controller )) {
				$controller = $dispatcher->getDefaultControllerName ();
			}
		}
		if (isset ( $module )) {
			$params ['module'] = $module;
		}
		$params ["controller"] = $controller;
		$params ["action"] = $action;
		$this->setGotoRoute ( $params );
	}
	
	/**
	 * Build a URL based on a route
	 *
	 * @param  array   $urlOptions
	 * @param  string  $name Route name
	 * @param  boolean $reset
	 * @return void
	 */
	public function setGotoRoute(array $urlOptions = array(), $name = null, $reset = true) {
		if (! isset ( $urlOptions [SmartL_Application::URL_LANGUAGE_ID] )) {
			$languages = SmartL_Application::getInstance ()->getLanguages ();
			$urlOptions [SmartL_Application::URL_LANGUAGE_ID] = strtolower ( $languages [0]->getId () );
		}
		$params = array ("controller", "action" );
		foreach ( $params as $key ) {
			if (isset ( $urlOptions [$key] )) {
				$urlOptions [$key] = strtolower ( $urlOptions [$key] );
			}
		}
		parent::setGotoRoute ( $urlOptions, $name, $reset );
	}
}